package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijianhuawen.wjblog.common.domain.dos.CategoryDo;
import com.weijianhuawen.wjblog.common.domain.dos.TagDo;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-22 16:47
 */
public interface TagMapper extends BaseMapper<TagDo> {
    //根据标签名查询
    default TagDo selectByName(String tagName) {
        LambdaQueryWrapper<TagDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TagDo::getName, tagName);

        //查询
        return selectOne(wrapper);
    }

    //按照一定条件分页查询
    default Page<TagDo> selectPageList(Long current, Long size, String tagName, LocalDate startDate, LocalDate endDate) {
        //分页对象
        Page<TagDo> page = new Page<>(current, size);

        //构建分页查询条件
        LambdaQueryWrapper<TagDo> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .like(Objects.nonNull(tagName), TagDo::getName, tagName)
                .ge(Objects.nonNull(startDate), TagDo::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), TagDo::getCreateTime, endDate)
                .orderByDesc(TagDo::getCreateTime);

        return selectPage(page, wrapper);
    }

    //模糊查询
    default List<TagDo> searchLikeTag(String key) {
        //构建查询语句
        LambdaQueryWrapper<TagDo> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(TagDo::getName, key).orderByDesc(TagDo::getCreateTime);

        return selectList(wrapper);
    }

    // 根据标签id表批量进行查询
    default List<TagDo> selectListByTagIds(List<Long> tagIds) {
        return selectList(Wrappers.<TagDo>lambdaQuery()
                .in(TagDo::getId, tagIds));
    }

    // limit
    default List<TagDo> selectByLimit(Long size) {
        return selectList(Wrappers.<TagDo>lambdaQuery()
                .orderByDesc(TagDo::getCreateTime)
                .last(String.format("LIMIT %d", size)));
    }
}
